package com.englisheveryday.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

import com.englisheveryday.R;
import com.englisheveryday.constants.Constants;
import com.englisheveryday.media.SimpleMediaPlayerService;
import com.englisheveryday.media.SimpleMediaPlayerService.ServiceBinder;
import com.englisheveryday.utils.L;

/**
 * <pre>
 *       Launch Activity
 * </pre>
 * @author 马永华
 * @date 2012-10-17 下午2:44:39
 * @version 1.1
 */

public class LaunchActivity extends Activity {

	private Handler handler;
	private ImageView  logoView,fighting;
	private AlphaAnimation alphaAnimation;
	private TranslateAnimation translate;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.launch_activity);
		handler = new LaunchHandler();
		new Thread(new LaunchThread()).start();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}

	class LaunchHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case Constants.SYSTEM_INIT_FINISH : {
				final Intent intent = new Intent();
				intent.setClass(LaunchActivity.this, MainActivity.class);
				
				
				startActivity(intent);
				LaunchActivity.this.finish();
			/**	
				alphaAnimation = new AlphaAnimation(0,1);
				translate = new TranslateAnimation(0, 0, 0,-200);
				alphaAnimation.setDuration(2000);
				alphaAnimation.setFillAfter(true);
				logoView = (ImageView)findViewById(R.id.logo);
				fighting = (ImageView)findViewById(R.id.fighting);
				logoView.startAnimation(alphaAnimation);
				fighting.startAnimation(alphaAnimation);
				translate.setDuration(1000);
				translate.setFillAfter(true);
				alphaAnimation.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
					}
					@Override
					public void onAnimationRepeat(Animation animation) {
					}
					@Override
					public void onAnimationEnd(Animation animation) {
						logoView.startAnimation(translate);
					}
				});
				translate.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
					}
					@Override
					public void onAnimationRepeat(Animation animation) {
					}
					@Override
					public void onAnimationEnd(Animation animation) {
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						startActivity(intent);
						LaunchActivity.this.finish();
					}
				});
				*/
			}
			break;
			case Constants.SYSTEM_INIT_ERROR : {
				Toast.makeText(LaunchActivity.this, R.string.system_init_error, Toast.LENGTH_LONG).show();
			}
			break;
			}
			
		}
		
	}
	
	class LaunchThread implements Runnable {
		@Override
		public void run() {
			if(init())
			handler.sendEmptyMessage(Constants.SYSTEM_INIT_FINISH);
			else
			handler.sendEmptyMessage(Constants.SYSTEM_INIT_ERROR);
		}
		
	}
	
	// 系统初始化
	public boolean init() {
		//在SD卡上创建文件目录
		File file  = new File(Constants.BASEDIR);
		if(!file.exists()||!file.isDirectory()){
			file.mkdirs();
			L.d("创建文件目录");
		}
		InputStream in = getResources().openRawResource(R.raw.englishdata);
		if(in==null)
			return false;
		File romEnglishData = new File("/data/data/com.englisheveryday/englishdata.db");
		if (romEnglishData.exists()) {
			L.d("文件已经存在");
			return true;
		}
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(romEnglishData);
			byte[] buffer = new byte[255];
			int length = 0;
			while ((length = in.read(buffer)) !=-1) {
				fos.write(buffer, 0, length);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (fos != null)
					fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Log.d("初始化", "初始化成功");
		return true;
	}
}

